namespace hjc {

    export class ChunkMap extends eui.Component {
        public static NAME = '块图地图';
        private $thumbnail: eui.Image;
        private $chunksLayer: egret.DisplayObjectContainer;
        private $url: string;
        private $chunkWidth: number;
        private $chunkHeight: number;
        private $colNum: number;
        private $rowNum: number;
        private $chunks: Chunk[] = [];
        private $type: string;
        public get url(): string { return this.$url; }
        public get chunkWidth(): number { return this.$chunkWidth; }
        public get chunkHeight(): number { return this.$chunkHeight; }
        public get colNum(): number { return this.$colNum; }
        public get rowNum(): number { return this.$rowNum; }

        constructor(options?: ChunkMapConfig) {
            super();
            this.$thumbnail = ui.setDisplay(ui.createImg(), null, this);
            this.touchEnabled = false;
            this.touchChildren = false;
            this.$chunksLayer = ui.setDisplay(new egret.DisplayObjectContainer, { touchEnabled: false, touchChildren: false }, this);
            if (!!options) this.load(options);
        }

        //清空地图数据
        public clear(): ChunkMap {
            this.$url = '';
            this.$chunkWidth = this.$chunkHeight = this.$colNum = this.$rowNum = this.width = this.height = 0;
            this.$thumbnail.source = null;
            this.$chunksLayer.removeChildren();
            return this;
        }
        //重设配置
        public load(config: ChunkMapConfig): ChunkMap {
            if (this._checkConfig(config)) {
                this.clear();
                this.name = !!config.name ? config.name : ChunkMap.NAME;
                this.$type = !!config.type ? config.type : 'jpg';
                this.$url = config.url;
                this.$chunksLayer.width = config.width;
                this.$chunksLayer.height = config.height;
                this.width = config.width;
                this.height = config.height;
                this.$chunkWidth = utils.num(config.chunkSize, config.chunkWidth);
                this.$chunkHeight = utils.num(config.chunkSize, config.chunkHeight);
                if (!!config.thumbnail) {
                    this.$thumbnail.source = config.thumbnail;
                    this.$thumbnail.width = config.width;
                    this.$thumbnail.height = config.height;
                }
                this.$colNum = config.colNum;
                this.$rowNum = config.rowNum;
                this._addChunks();
                if (config.cache !== false) this.$chunksLayer.cacheAsBitmap = true;
            }
            return this;
        }

        private _addChunks(): void {
            let [w, h] = [this.$chunkWidth, this.$chunkHeight];
            for (let row = 0; row < this.$rowNum; row++) {
                for (let col = 0; col < this.$colNum; col++) {
                    let url = this.$url + this._suffix(row, col);
                    let index = row * this.$rowNum + col;
                    let chunk = this.$chunks[index];
                    if (!chunk) {
                        chunk = game.claim(Chunk, null, ui.setDisplay(new eui.Image(), {
                            x: col * w, y: row * h, name: '图块'
                        }, this.$chunksLayer), url);
                    } else chunk.init(null, null, url);
                    chunk.position.x = col * w + w / 2;
                    chunk.position.y = row * h + h / 2;
                    chunk.size.w = w;
                    chunk.size.h = h;
                }
            }
        }

        protected _suffix(row: number, col: number): string {
            return `${row}_${col}.${this.$type}`;
        }


        private _checkConfig(config?: ChunkMapConfig): boolean {
            let func = 'ChunkMap.load()';
            if (isEmpty(config)) { hjc.wrong(func, 'is empty'); return false; }
            if (!config.url) { hjc.require(func, 'string', 'url'); return false; }
            if (!Math.isPosInt(config.colNum) || !Math.isPosInt(config.rowNum)) { hjc.require(func, 'positive integer', 'colNum', 'rowNum'); return false; }
            if (!Math.isPositive(config.width) || !Math.isPositive(config.height)) { hjc.require(func, 'positive number', 'width', 'height'); return false; }
            if (!Math.isPositive(config.chunkSize) && (!Math.isPositive(config.chunkWidth) || !Math.isPositive(config.chunkHeight))) { hjc.require(func, 'positive number', 'chunkSize', 'chunkWidth&Height'); return false; }
            return true;
        }
    }
}


